package com.sdl.auth.service.controller;

import com.sdl.common.base.enums.ErrorCodeEnum;
import com.sdl.common.base.exception.BusinessException;
import com.sdl.common.wrapper.WrapMapper;
import com.sdl.common.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;

/**
 * @program: flowerPaaS
 * @description: TODO 异常全局拦截
 * @author: songdeling
 * @create: 2020-06-17
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    //@Autowired
    //private TaskExecutor taskExecutor;
    //@Value("${spring.profiles.active}")
    //String profile;
    //@Value("${spring.application.name}")
    //String applicationName;
    //@Resource
    //private MdcExceptionLogFeignApi mdcExceptionLogFeignApi;

    /**
     * 参数非法异常.
     *
     * @param e the e
     *
     * @return the wrapper
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper illegalArgumentException(IllegalArgumentException e) {
        log.error("参数非法异常={}", e.getMessage(), e);
        return WrapMapper.wrap(ErrorCodeEnum.GL99990100.code(), e.getMessage());
    }

    /**
     * 业务异常.
     *
     * @param e the e
     *
     * @return the wrapper
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper businessException(BusinessException e) {
        log.error("系统业务异常={}", e.getMessage(), e);
        return WrapMapper.wrap(e.getCode() == 0 ? Wrapper.ERROR_CODE : e.getCode(), e.getMessage());
    }

    /**
     * 参数非法异常.
     *
     * @param e the e
     *
     * @return the wrapper
     */
    @ExceptionHandler(DataAccessException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper dataAccessException(DataAccessException e) {
        log.error("操作数据库异常={}", e.getMessage(), e);
        return WrapMapper.wrap(ErrorCodeEnum.GL99990003.code(), e.getMessage());
    }

    /**
     * 系统异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper exception(Exception e) {
        log.error("系统异常={}", e.getMessage(), e);
        return WrapMapper.wrap(ErrorCodeEnum.GL99990003.code(), e.getMessage());
    }


    /**
     * 全局异常保存.后面再用
     *
     * @param e the e
     *
     * @return the wrapper
     */
    //@ExceptionHandler(Exception.class)
    //@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    //@ResponseBody
    //public Wrapper exception(Exception e) {
    //    log.info("保存全局异常信息 ex={}", e.getMessage(), e);
    //    taskExecutor.execute(() -> {
    //        GlobalExceptionLogDto globalExceptionLogDto = new GlobalExceptionLogDto().getGlobalExceptionLogDto(e, profile, applicationName);
    //        mdcExceptionLogFeignApi.saveAndSendExceptionLog(globalExceptionLogDto);
    //    });
    //    return WrapMapper.error();
    //}
}
